import { Optional } from '@angular/core';
import * as Cookies from 'js-cookie';

import { IToken, IStore } from '../auth.interface';
import { AuthConfig } from '../../config/auth.config';

export class CookieService implements IStore {

  constructor(
    @Optional() private cfg: AuthConfig
  ) { }

  get(key: string): IToken {
    if (this.cfg.token_send_place === 'cookie') {
      return {
        token: Cookies.get(key)
      };
    }
    return Cookies.getJSON(key);
  }

  set(key: string, value: IToken, optinos: any): boolean {
    if (this.cfg.token_send_place === 'cookie') {
      Cookies.set(key, value.token, optinos);
    } else {
      Cookies.set(key, value, optinos);
    }
    return true;
  }

  remove(key: string, options: any) {
    Cookies.remove(key, options);
  }
}
